/**************************************************************************************
 *
 * @(#) CorsConfig.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.config.mvc
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.config.mvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description: todo 本地运行 处理 OPTIONS请求 跨域问题,非本地环境可注释
 * @author: huangAn
 * @createDate: 2025/04/29
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer{

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://localhost:5173")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 必须包含OPTIONS
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .maxAge(3600); // 预检请求缓存时间
            }
        };
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("*")
                // 显式声明允许的请求头（如 Authorization）
                .allowedHeaders("Authorization", "Content-Type", "X-Requested-With")
                .allowCredentials(true)
                .maxAge(1800);
    }

}




